*==========================================================================================================================================================
*
*	Local Projections
*
*==========================================================================================================================================================


set more off

qui {


*-------------------------------------------------------------------------------------------------------------
*	SETTINGS
*-------------------------------------------------------------------------------------------------------------

* Variables
local vars "scont_esp_al scont_bar_fe scont_val_fe"  

* irf horizon
local H 5
local F 5

* lags
local L 2
local LS `L'

*-------------------------------------------------------------------------------------------------------------



*_________ GET DATA ____________
use "data/working files/prepared.dta", clear 


*_______ DEBASEMENT GRAPH _______
gen disa = 0 if disaster==1

la var scont_esp_al "Silver per unit of account"
la var disa "Maritime disasters"

foreach k in scont_esp_al scont_bar_fe scont_val_fe {
	gen jnk1 = `k' if year == 1531
	egen jnk2 = min(jnk1)
	replace `k'  = `k' /jnk2 * 100
	drop jnk1 jnk2
	}


*____ DECLARE TIME SERIES _____
gen id =1
tsset id year, yearly

*_________________ LOCAL PROJECTIONS _________________

local count = 0
foreach v of local vars {
local count = `count'+1
n di "Variable `v'"


***_________ CONTROLS

local indep	" gdp_r_adj_gr cpi_sil_gr wage_sil_gr itsN_gr" 

forvalues i=0/`L'{
	local indep_`i' = subinstr("`indep'"," "," l`i'_",.)
	}
	
local rhs ""
forvalues i = 1/`L'{
	local rhs "`rhs'`indep_`i''"
	}
	
	local not l0_`v'_gr l0_itsN_gr 
	local rhs: list rhs- not
	
	


	***_________ IMPULSE RESPONSE and CONFIDENCE INTERVAL variables
	gen irf_`v' = . if _n<=`H'+1
	
	gen se_`v' = . if _n<=`H'+1

	gen sig_`v' = . if _n<=`H'+1
	gen ci_up_`v' = . if _n<=`H'+1
	gen ci_lo_`v' = . if _n<=`H'+1
	gen ci_up90_`v' = . if _n<=`H'+1
	gen ci_lo90_`v' = . if _n<=`H'+1
			
	
	
	***_________ ESTIMATE COEFFICIENTS
	
	forvalues y = 0/`H' {
		n display "projection `y'"
		
		newey `v'_gr`y' l(1/`LS').`v'_gr l(-`F'/`LS').perclossN_vellon l(-`F'/`LS').perclossN l(-`F'/`LS').percloss_salvN `rhs' l(-`F'/`LS').percloss_pirN l(0/`LS').temperature l(0/`LS').warbeu l(0/`LS').warbciv, lag(5) force 

		* store IMPULSE RESPONSES and STANDARD ERRORS	
		sum `v'
		if (`r(N)' >= 100) {
			lincom perclossN 
			}
		else {
			lincom percloss_netN
			}
		replace irf_`v' = r(estimate) if _n==`y'+1 /* coefficients */
		replace se_`v' = r(se) if _n==`y'+1 /* standard errors */
		
		} //horizon			
	} // vars


	
local count = 0
foreach v of local vars {
local count = `count'+1
n di "Variable `v'"

	***_________ CONFIDENCE INTERVALS
	replace ci_up_`v' = irf_`v' + 1*se_`v' if _n <= `H'+1
	replace ci_lo_`v' = irf_`v' - 1*se_`v' if _n <= `H'+1
	replace ci_up90_`v' = irf_`v' + 1.64*se_`v' if _n <= `H'+1
	replace ci_lo90_`v' = irf_`v' - 1.64*se_`v' if _n <= `H'+1
	
	replace sig_`v' =  irf_`v' if ci_up90_`v' < 0 | ci_lo90_`v' > 0

}



* plot prerequisites
gen time = _n-1 if(_n<=`H'+1) 
drop if time > `H'
keep time irf_* ci_up_* ci_lo_*  ci_up90_* ci_lo90_* se_* sig_*
order irf_* ci_up_* ci_lo_*  time ci_up90_* ci_lo90_* sig_*
drop if time == . 
gen zero = 0 if time!=.



local zline "medthin"
local irfline "medthick"
local msize "large"

local lsize "medlarge"
local tsize "medlarge"
local legsize "medlarge"

twoway  (line zero time, lcolor(black) lwidth(`zline')) ///
		(scatter irf_scont_esp_al time, color(black) msymbol(circle_hollow) msize(`msize')) ///
		(line irf_scont_esp_al time, lcolor(black) lpattern(dot) lwidth(`irfline')) ///
		(scatter sig_scont_esp_al time, color(black) msymbol(circle) msize(`msize')) ///
		(scatter irf_scont_bar_fe time, color(gs8) msymbol(diamond_hollow) msize(`msize')) ///
		(line irf_scont_bar_fe time, lcolor(gs8) lpattern(dot) lwidth(`irfline')) ///
		(scatter sig_scont_bar_fe time, color(gs8) msymbol(diamond) msize(`msize')) ///
		(scatter irf_scont_val_fe time, color(gs5) msymbol(triangle_hollow) msize(`msize')) ///
		(line irf_scont_val_fe time, lcolor(gs5) lpattern(dot) lwidth(`irfline')) ///
		(scatter sig_scont_val_fe time, color(gs5) msymbol(triangle) msize(`msize')), ///
		title("Silver per unit of account", size(`tsize')) xtitle("Year", size(`lsize')) ytitle("Percent", height(3) size(`lsize')) xlabel(,labsize(`lsize') ) ylabel(-1(.2)0, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
legend(order(2 5 8) size(`legsize') pos(7) ring(0) col(1) lab(2 "Castilian Maravedí") lab(5 "Barcelona Sous") lab(8 "Valencian Sous") region(lcolor(white))) ///
		graphregion(color(white)) plotregion(color(white)) name(g1, replace) nodraw
		
		graph combine g1, name(LP_disag_debase_all, replace) cols(1) imargin(medsmall) plotregion(color(white)) graphregion(color(white)) 
graph display LP_disag_debase_all, ysize(2.1) xsize(2.5) scale(1) 
graph export "results/FigureB4.pdf", replace 	


} // qui


*_________________________________________

			

			
